package com.example.projects;

import java.io.File;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

import com.facebook.android.Facebook;



public class Main extends CPT3004Menu {
	final public static String BARCODE = "com.example.projects.MESSAGE";
	private String contents;
	private ViewPager _mViewPager;
	private ViewPagerAdapter _adapter;
	
    private Facebook fb;
    private SharedPreferences mPrefs;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //this.setTheme(R.style.AppTheme);
        File folder = new File(getFilesDir().getAbsolutePath());
        if (!folder.exists()) {
        	folder.mkdir();
        }
        setContentView(R.layout.main_menu);
        setUpView();
        setTab();
        
        // set up facebook
        fb = ((CPT3004)getApplication()).getFacebook();
		if(fb == null) {
    		// create new facebook
    		fb = new Facebook(((CPT3004)getApplication()).getAppID());
    		
    		// prepare facebook for use
    		mPrefs = getSharedPreferences("", MODE_PRIVATE);
        	String access_token = mPrefs.getString("access_token", null);
        	long expires = mPrefs.getLong("access_expires", 0);
        	
        	// reuse old token if possible
        	if (access_token != null) {
    	    	fb.setAccessToken(access_token);
	        	if(expires != 0) {
	        		fb.setAccessExpires(expires);
	        	}
	        	fb.extendAccessTokenIfNeeded(this, null);
        	}
    		// store facebook
    		((CPT3004)getApplication()).setFacebook(fb);
    	}
        
    }
    

    
	    //The control of scanBarcode button
	public void scanBarcode(View view) {

	
		//String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
	    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
	    //using scan mode and be able to scan product mode
	    intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
	    //start activity and go to zxing api goes back to onActivityResult later.
	    startActivityForResult(intent, 0);
	}
	
	//come back from the zxing 
	@Override    
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
 		if (requestCode == 1) {
	        if (resultCode == RESULT_OK) {
	        	//result after the scan (barcode)
	            contents = intent.getStringExtra("SCAN_RESULT");
	            //String format = intent.getStringExtra("SCAN_RESULT_FORMAT");

	            AccessDatabase ad = new AccessDatabase(this);
	            ad.execute(contents, getFilesDir().getAbsolutePath(), "main");

	        }       
		}
 		else {
 			fb.authorizeCallback(requestCode, resultCode, intent);
 		}
	}
	
	
	public void toEdit(View view){
        Intent intent = new Intent(this, Edit.class);
        //send contents(the barcode) to the product class
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
	}
	
    private void setUpView(){    	
    	_mViewPager = (ViewPager) findViewById(R.id.viewPager);
    	_adapter = new ViewPagerAdapter(getApplicationContext(),getSupportFragmentManager());
    	_mViewPager.setAdapter(_adapter);
		_mViewPager.setCurrentItem(0);
    }
    private void setTab(){
    	_mViewPager.setOnPageChangeListener(new OnPageChangeListener(){
			    		
			public void onPageScrollStateChanged(int position) {}
			public void onPageScrolled(int arg0, float arg1, int arg2) {}
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				switch(position){
				case 0:
					findViewById(R.id.first_tab).setVisibility(View.VISIBLE);
					findViewById(R.id.second_tab).setVisibility(View.INVISIBLE);
					break;
					
				case 1:
					findViewById(R.id.first_tab).setVisibility(View.INVISIBLE);
					findViewById(R.id.second_tab).setVisibility(View.VISIBLE);
					break;
				}
			}
			
		});

    }
    

}